'use strict';
const fs = require('fs');
const path = require('path');
var mkdirs = require('mkdirs');
const Service = require('egg').Service;

class createQrcodeService extends Service {
    constructor(ctx) {
        super(ctx);

    }
    async createroadQr(qrcode_id) {
        var QRCode = require('qrcode');
        console.log("********************进入创建二维码************************")
        console.log(qrcode_id)
        let file_dir = new Date().Format("yyyyMMdd");
        let file_name = new Date().getTime() + String(Math.ceil(Math.random() * 1000));
        file_name = file_name.substring(4, file_name.length) + ".png";
        if (!fs.existsSync(path.join(this.app.config.uploadDir))) {
            fs.mkdirSync(path.join(this.app.config.uploadDir))
        }
        if (!fs.existsSync(path.join(this.app.config.uploadDir,`qrcode/`))) {
            fs.mkdirSync(path.join(this.app.config.uploadDir,`qrcode/`))
        }
        if (!fs.existsSync(path.join(this.app.config.uploadDir, `qrcode/${file_dir}`))) {
            fs.mkdirSync(path.join(this.app.config.uploadDir, `qrcode/${file_dir}`))
        }
        var paths = this.app.config.uploadDir + `qrcode/${file_dir}/${file_name}`;
        return new Promise(function (resolve, reject) {
            QRCode.toFile(paths, 'https://wiki.fudengtech.com/?miniapptype=tongxin&qrcode_id=' + qrcode_id, {
                color: {
                    dark: '#000000',  // Blue dots
                    light: '#0000' // Transparent background
                },
                width: 500
            }, function (err) { 
                if (err) {
                    resolve({
                        errno: '1',
                        msg: err
                    });
                    throw err;
                } else {
                    resolve({
                        errno: '0',
                        path: `/public/upload/qrcode/${file_dir}/${file_name}`,
                    });
                }
            });
        }, function (err) {
            console.log(err);
            resolve({
                errno: '1',
                msg: err
            });
        });
    }
    async addWater(urlImg) {
        var images = require("images");

        var waterImg = this.app.config.fileDir + "/public/img/qrlogo.png";
        let file_dir = new Date().Format("yyyyMMdd");
        let file_name = new Date().getTime() + String(Math.ceil(Math.random() * 1000));
        file_name = file_name.substring(4, file_name.length) + ".png";
        if (!fs.existsSync(path.join(this.app.config.uploadDir, `qrcode/${file_dir}`))) {
            fs.mkdirSync(path.join(this.app.config.uploadDir, `qrcode/${file_dir}`))
        }
        var paths = this.app.config.uploadDir + `qrcode/${file_dir}/${file_name}`;
        var dir = this.app.config.fileDir + urlImg;
        return new Promise(function (resolve, reject) {

            images(dir)                     //Load image from file
                //加载图像文件
                .size(500)                          //Geometric scaling the image to 400 pixels width
                //等比缩放图像到400像素宽
                .draw(images(waterImg), 200, 200)   //Drawn logo at coordinates (70,260)//为了遮住不该看的东西..
                //在(10,10)处绘制Logo
                .save(paths, {               //Save the image to a file,whih quality 50
                    quality: 50                    //保存图片到文件,图片质量为50
                });
            //源文件的位置
            var stats = fs.statSync(dir);
            if (!stats.isDirectory()) {
                fs.unlinkSync(dir);
                //console.log("删除文件"+dir+'/'+file+"成功");
            }
            resolve({
                errno: '0',
                path: `/public/upload/qrcode/${file_dir}/${file_name}`,
            });
        }, function (err) {
            console.log(err);
            resolve({
                errno: '1',
                msg: err
            });
        });


    }
}

module.exports = createQrcodeService;
